$(function () {
	var lastsel;
    $("#jqGrid").jqGrid({
        url: baseURL + 'dev/earnestmoneyorder/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '酒店名称', name: 'hotelName', index: 'hotel_name', width: 80 }, 						
			{ label: '房间号', name: 'hotelRoomName', index: 'hotel_room_name', width: 80 , editable: true}, 
			{ label: '姓名', name: 'name', index: 'name', width: 80 },
			{ label: '联系电话', name: 'mobile', index: 'mobile', width: 80 }, 			
			{ label: '押金(元)', name: 'price', index: 'price', width: 80 }, 			
			{ label: '退款金额(元)', name: 'refundFee', index: 'refund_fee', width: 80 }, 			
			{ label: '状态', name: 'status', width: 50, formatter: function(value, options, row){
				if(value == '2'){
					return '<span class="label label-danger">已退款</span>';
				}else if(value == '1'){
					return '<span class="label label-success">已支付</span>';
				}else{
					return '<span class="label label-warning">未支付</span>';
				}
			}},
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
			{ label: '操作', name: 'createTime', width: 80, formatter: function(value, options, row){
				return '<a class="btn btn-primary" style="padding: 1px 12px;" onclick="vm.refundMoney('+row.id+','+row.price+')">退款</a>' 
				+ '&nbsp;&nbsp;<a class="btn btn-primary" style="padding: 1px 12px;" onclick="vm.giveCoupon('+row.id+',\''+row.name+'\')">赠送早餐券</a>';
			}}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        editurl : baseURL + 'dev/earnestmoneyorder/updateHotelRoom',
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        },
        ondblClickRow : function(id) {
        	if (id && id !== lastsel) {
        		$("#jqGrid").jqGrid('restoreRow', lastsel);
        		$("#jqGrid").jqGrid('editRow', id, true);
	            lastsel = id;
	        }
        	//原选中行ID
            //var oldSelectRowId = $("#selectRowId").val();
            //if (oldSelectRowId != null && oldSelectRowId != "" && oldSelectRowId.length > 0) {
               // $("#fieldGrid").jqGrid('saveRow', oldSelectRowId);//保存上一行
           // }

            //当前选中行
            //$("#selectRowId").val(id);//临时存储当前选中行
            //$("#fieldGrid").jqGrid('editRow', id);
            //$("#jqGridPager").jqGrid('editRow', id, { keys: true, focusField: 1 });

            //$("#jqGridPager").setColProp("sort",{editable:true});
            //$("#jqGridPager").editRow(15,true);
        	//jQuery("#jqGridPager").jqGrid('editRow', 15);
            
	    }
    });
    
	laydate.render({
		elem: '#startTime' //指定元素
		,done: function(value, date, endDate){
			console.log(value); //得到日期生成的值，如：2017-08-18
		    vm.q.startTime = value;
		}
	});
	laydate.render({
		elem: '#endTime' //指定元素
		,done: function(value, date, endDate){
			console.log(value); //得到日期生成的值，如：2017-08-18
		    vm.q.endTime = value;
		    vm.reload();
		    vm.statTotalMoney();
		}
	});
});

//获取当前时间，格式YYYY-MM-DD
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}
var currdate = getNowFormatDate();

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            name: null,
            mobile: null,
			startTime:"",
			endTime:""
        },
		showList: true,
		title: null,
		isday:true,
		money1:0,
		money2:0,
		money3:0,
		earnestMoneyOrder: {},
		hotelNameList:[],
		hotelRoomNameList:[]
	},
	mounted:function(){
	    console.log("页面加载完成")
	    this.statTotalMoney();
	},
	methods: {
		query: function () {
			vm.reload();
			vm.statTotalMoney();
		},
		statTotalMoney: function(){
			var that = this;
			
			$.get(baseURL + "dev/earnestmoneyorder/statTotalMoney",{'startTime':that.q.startTime,'endTime':that.q.endTime}, function(r){
				
				
				that.q.startTime = r.startTime;
				that.q.endTime = r.endTime;
				if(that.q.startTime == "" || that.q.endTime == ""){
					that.isday = true;
				}else if(that.q.startTime == that.q.endTime && currdate == that.q.startTime){
					that.isday = true;
				}else{
					that.isday = false;
				}
				console.log("-------------------------");
				console.log(that.isday);
                var memberlist = r.moneylist;
                console.log(memberlist);
                that.money1 = memberlist[0] == null ? 0 : memberlist[0];
                that.money2 = memberlist[1] == null ? 0 : memberlist[1];
                that.money3 = memberlist[2] == null ? 0 : memberlist[2];
            });
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.earnestMoneyOrder = {};
		},
		update: function (id) {
			
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.earnestMoneyOrder.id == null ? "dev/earnestmoneyorder/save" : "dev/earnestmoneyorder/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.earnestMoneyOrder),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "dev/earnestmoneyorder/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "dev/earnestmoneyorder/info/"+id, function(r){
                vm.earnestMoneyOrder = r.earnestMoneyOrder;
                vm.getHotelNameList();
            });
		},
		getHotelNameList: function(){
			$.get(baseURL + "dev/hotel/queryHotelNameList", function(r){
                vm.hotelNameList = r.list;
            });
		},
		getHotelRoomNameList: function(){
			var hotelId = this.earnestMoneyOrder.hotelId;
			console.log(hotelId)
			$.get(baseURL + "dev/hotelroom/queryHotelRoomNameList?hotelId="+hotelId, function(r){
                vm.hotelRoomNameList = r.list;
            });
		},
		refundMoney: function (id,price) {
			if(id == null){
				return ;
			}
			price=prompt("请输入退款金额",price)
			if (price!=null && price!=""){
				$.ajax({
					type: "POST",
				    url: baseURL + "dev/earnestmoneyorder/refundMoney?id="+id+"&price="+price,
                    contentType: "application/json",
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			}
			
		},
		giveCoupon: function (id,name) {
			parent.layer.open({
			  type: 2,
			  title: name,
			  shadeClose: true,
			  shade: 0.8,
			  area: ['50%', '50%'],
			  content: "modules/dev/breakfastcoupon2.html?orderId="+id+"&type=2"
			}); 
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'name': vm.q.name,'mobile':vm.q.mobile,'startTime':vm.q.startTime,'endTime':vm.q.endTime},
                page:page
            }).trigger("reloadGrid");
		}
	}
});